//
//  ControllerForCollect.m
//  ProjectA
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ControllerForCollect.h"    //C
#import "ControllerForClickItem.h"

#import "CellForCollect.h"  //V
#import "ViewForCollectFooter.h"

#import "ModelForSection1.h"    //M


@interface ControllerForCollect ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrForModel;

@end

@implementation ControllerForCollect
- (void)dealloc {
    
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    
    [_tableView release];
    [_arrForModel release];

    [super dealloc];
}
- (void)setNightDayModel {
    [super setNightDayModel];
    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
    
}
- (void)setLightDayModel {
    [super setLightDayModel];
    self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
}
- (void)viewWillAppear:(BOOL)animated {
    
    [self handleData];
    
    [self.tableView reloadData];
    
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
        
    }
    
    
}
- (void)viewWillDisappear:(BOOL)animated {
    
    self.barBackgroundView.backgroundColor = L_COLOR_CLEAR;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self settingsNavigationController];
    
    [self createTableView];
}
- (void)handleData {
    self.arrForModel = [NSMutableArray array];
    self.arrForModel = [[DataBaseHandler shareDataBaseHandler] selectAll].mutableCopy;
}
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.tableView registerClass:[CellForCollect class] forCellReuseIdentifier:@"pool"];
    [self.tableView registerClass:[ViewForCollectFooter class] forHeaderFooterViewReuseIdentifier:@"poolForFooter"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.frame.size.width / 3.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (self.arrForModel.count) {
        self.tableView.scrollEnabled = YES;
        return 0;
    } else {
        self.tableView.scrollEnabled = NO;
        return 400;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrForModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellForCollect *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.backgroundColor = [UIColor clearColor];
    [cell passData:[self.arrForModel objectAtIndex:indexPath.row]];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    ViewForCollectFooter *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForFooter"];
    view.label.text = @"这里什么也没有";
    return view;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ControllerForClickItem *item = [[ControllerForClickItem alloc] init];
    ModelForSection1 *model = [self.arrForModel objectAtIndex:indexPath.row];
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@", model.spot_id];
    item.spot_id = url;
    item.model = [self.arrForModel objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:item animated:YES];
    [item release];
    self.tabBarController.tabBar.hidden = YES;
   
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (UITableViewCellEditingStyleDelete == editingStyle) {

        ModelForSection1 *model = [self.arrForModel objectAtIndex:indexPath.row];
        [[DataBaseHandler shareDataBaseHandler] deleteWithId:model.spot_id];
        [self.arrForModel removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView reloadData];
    }
    
}

- (void)settingsNavigationController {
    
    self.navigationItem.title = @"我的收藏";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(heise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)];

}

- (void)houtuiAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    //self.navigationController.navigationBar.hidden = YES;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
